//
//  UIAViewController.m
//  UIA
//
//  Created by sk on 11-7-28.
//  Copyright 2011 sk. All rights reserved.
//

#import "UIAViewController.h"
#import "BIDDateView.h"
#import "BIDPinViewController.h"
#import "BIDUserInputViewController.h"
#import "BIDUserSaveToInternet.h"
#import"myimgeview.h"
#import "BIDDeleteBill.h"
#import "BIDModifyView.h"
#import "BIDHistoryClock.h"
#define RADIUS 100.0
#define PHOTONUM 5
#define PHOTOSTRING @"icon"
#define TAGSTART 1000
#define TIME 1.5
#define SCALENUMBER 1.25
int array [PHOTONUM][PHOTONUM] =
{
	{0,1,2,3,4},
	{4,0,1,2,3},
	{3,4,0,1,2},
	{2,3,4,0,1},
	{1,2,3,4,0}
};
@implementation UIAViewController


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
CATransform3D rotationTransform1[PHOTONUM];
//暂停播放
-(void)NSTimeInterval
{
     [playmusic stop];
}
-(void)dismAlter:(NSTimer *)time
{

}

- (void)viewDidLoad
{

       [self play];

    [super viewDidLoad];
	//addview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Defaul11t.png"]];
	//NSLog(@"%@",NSStringFromCGRect(addview.bounds));
	/*
	addview.layer.anchorPoint = CGPointMake(0.5, 0.5);
	CGFloat centerY = self.view.center.y + (self.view.bounds.size.height/2);
	//addview.center = CGPointMake(self.view.center.x, centerY);
	addview.layer.position = CGPointMake(self.view.center.x, self.view.center.y);
	 */
	//self=[[UIAViewController alloc] initWithNibName:@"UIAViewController" bundle:nil];
	//self.view.backgroundColor = [UIColor clearColor];
	UIImageView *backview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"121.png"]];
	//backview.frame = self.view.frame;
	backview.center = CGPointMake(backview.center.x, backview.center.y - 10);
	
	backview.alpha = 1;
	[self.view addSubview:backview];
	
    NSArray *textArray = [NSArray arrayWithObjects:@"记一笔",@"查看数据",@"管理数据",@"用户设置",@"消费提醒",nil];
    
	float centery = self.view.center.y - 50;
	float centerx = self.view.center.x;
    
	for (int i = 0;i<PHOTONUM;i++ ) 
	{
		float tmpy =  centery + RADIUS*cos(2.0*M_PI *i/PHOTONUM);
		float tmpx =	centerx - RADIUS*sin(2.0*M_PI *i/PHOTONUM);
		myimgeview *addview1 =	[[myimgeview alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d",PHOTOSTRING,i]] text:[textArray objectAtIndex:i]];
        addview1.frame = CGRectMake(0.0, 0.0,120,140);
		[addview1 setdege:self];
		addview1.tag = TAGSTART + i;
		addview1.center = CGPointMake(tmpx,tmpy);
		rotationTransform1[i] = CATransform3DIdentity;	
		
		//float Scalenumber =atan2f(sin(2.0*M_PI *i/PHOTONUM));
		float Scalenumber = fabs(i - PHOTONUM/2.0)/(PHOTONUM/2.0);
		if (Scalenumber<0.3) 
		{
			Scalenumber = 0.4;
		}
		CATransform3D rotationTransform = CATransform3DIdentity;
		rotationTransform = CATransform3DScale (rotationTransform, Scalenumber*SCALENUMBER,Scalenumber*SCALENUMBER, 1);		
		addview1.layer.transform=rotationTransform;		
		[self.view addSubview:addview1];
		
	}
	currenttag = TAGSTART;


     
    [self startAnimation];


}
-(void)Clickup:(NSInteger)tag
{
	NSLog(@"点击TAG%d:",tag);
//	int = currenttag - tag;
	if(currenttag == tag)
	{
		if (tag==1000)
        {
              [self stop];
            
            BIDUserInputViewController * nextview = [[BIDUserInputViewController alloc] init];
              UIView *view=self.navigationController.view;
            [self pageCurlAnmation:view];
            [self.navigationController pushViewController:nextview  animated:YES];
            
            NSLog(@"记一笔");
        }
        if (tag==1001)
         {    [self stop];

            NSLog(@"查看数据");
			UITabBarController *bar=[[UITabBarController alloc] init];
			
			BIDDateView *bew=[[BIDDateView alloc] init];
			bew.view.backgroundColor = [UIColor whiteColor];
			BIDPinViewController * pinView = [[BIDPinViewController alloc] init];
       UIImage *dateimage = [UIImage imageNamed:@"search.png"];
       UIImage *pinimage = [UIImage imageNamed:@""];
			bew.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"时间" image:dateimage tag:0];
            

			  pinView.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"饼图" image:pinimage tag:0];
			bar.viewControllers=[NSArray arrayWithObjects:bew,pinView, nil];
			[self.navigationController pushViewController:bar animated:YES];
        }
        if (tag==1002)
        {
                      [self stop];

          UITabBarController *bar=[[UITabBarController alloc] init];

          BIDDeleteBill *delete=[[BIDDeleteBill alloc] init];
          delete.view.backgroundColor = [UIColor whiteColor];
          BIDModifyView * modify = [[BIDModifyView alloc] init];

          bar.viewControllers=[NSArray arrayWithObjects:delete,modify, nil];

       UIImage *mangerimage = [UIImage imageNamed:@"stock-person"];
       UIImage *mangerimage1 = [UIImage imageNamed:@"administrator-1"];

			delete.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"帐单的管理" image:mangerimage tag:1];
			  modify.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"类别管理" image:mangerimage1 tag:1];



              UIView *view=self.navigationController.view;
            [self pageCurlAnmation:view];

			[self.navigationController pushViewController:bar animated:YES];
              
                        NSLog(@"管理数据");

        }
        if (tag==1003)
        {
              [self stop];

                  NSLog(@"用户设置");
      
             BIDUserSaveToInternet *next=[[BIDUserSaveToInternet alloc] initWithNibName:@"BIDUserSave" bundle:nil];
            UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:next];
        [self presentViewController:nav animated:YES completion:nil];
//         UIView *view=self.navigationController.view;
//            [self pageCurlAnmation:view];
////       [nav.navigationController pushViewController:next animated:YES];

//        [self.navigationController pushViewController:next animated:NO];

        
        }
        if (tag==1004)
        {

              [self stop];

            NSLog(@"other");
            BIDHistoryClock *next=[[BIDHistoryClock alloc]init];
            [self.navigationController pushViewController:next animated:YES];
        }
        
     
		return;
	}
	int t = [self getblank:tag];
//	NSLog(@"%d",t);
	int i = 0;
	for (i = 0;i<PHOTONUM;i++ ) 
	{
		
		UIImageView *imgview = (UIImageView*)[self.view viewWithTag:TAGSTART+i];
		[imgview.layer addAnimation:[self moveanimation:TAGSTART+i number:t] forKey:@"position"];
		[imgview.layer addAnimation:[self setscale:TAGSTART+i clicktag:tag] forKey:@"transform"];
		
		int j = array[tag - TAGSTART][i];
		float Scalenumber = fabs(j - PHOTONUM/2.0)/(PHOTONUM/2.0);
		if (Scalenumber<0.3) 
		{
			Scalenumber = 0.4;
		}
//		CATransform3D dtmp = CATransform3DScale(rotationTransform1[i],Scalenumber*SCALENUMBER, Scalenumber*SCALENUMBER, 1.0);
		//imgview.layer.transform=dtmp;
		
	//	imgview.layer.needsDisplayOnBoundsChange = YES;
	
          }
	currenttag = tag;

    
   
//	[self performSelector:@selector(setcurrenttag) withObject:nil afterDelay:TIME];
}
-(void)setcurrenttag
{
	int i = 0;
	for (i = 0;i<PHOTONUM;i++ ) 
	{
		
		UIImageView *imgview = (UIImageView*)[self.view viewWithTag:TAGSTART+i];		
		int j = array[currenttag - TAGSTART][i];
		float Scalenumber = fabs(j - PHOTONUM/2.0)/(PHOTONUM/2.0);
		if (Scalenumber<0.3) 
		{
			Scalenumber = 0.4;
		}
		CATransform3D dtmp = CATransform3DScale(rotationTransform1[i],Scalenumber*SCALENUMBER, Scalenumber*SCALENUMBER, 1.0);
		imgview.layer.transform=dtmp;
		
		//	imgview.layer.needsDisplayOnBoundsChange = YES;
	}
}

-(CAAnimation*)setscale:(NSInteger)tag clicktag:(NSInteger)clicktag
{
	
	
	int i = array[clicktag - TAGSTART][tag - TAGSTART];
	int i1 = array[currenttag - TAGSTART][tag - TAGSTART];
	float Scalenumber = fabs(i - PHOTONUM/2.0)/(PHOTONUM/2.0);
	float Scalenumber1 = fabs(i1 - PHOTONUM/2.0)/(PHOTONUM/2.0);
	if (Scalenumber<0.3) 
	{
		Scalenumber = 0.4;
	}
	//UIImageView *imgview = (UIImageView*)[self.view viewWithTag:tag];
	CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform"];
	animation.duration = TIME;
	animation.repeatCount =1;
	
	
   CATransform3D dtmp = CATransform3DScale(rotationTransform1[tag - TAGSTART],Scalenumber*SCALENUMBER, Scalenumber*SCALENUMBER, 1.0);
	animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DScale(rotationTransform1[tag - TAGSTART],Scalenumber1*SCALENUMBER,Scalenumber1*SCALENUMBER, 1.0)];
	animation.toValue = [NSValue valueWithCATransform3D:dtmp ];
	animation.autoreverses = NO;	
	animation.removedOnCompletion = NO;
	animation.fillMode = kCAFillModeForwards;
	//imgview.layer.transform=dtmp;
	
	return animation;
}

-(CAAnimation*)moveanimation:(NSInteger)tag number:(NSInteger)num
{
	// CALayer
	UIImageView *imgview = (UIImageView*)[self.view viewWithTag:tag];
    CAKeyframeAnimation* animation;
    animation = [CAKeyframeAnimation animation];	
	CGMutablePathRef path = CGPathCreateMutable();
	NSLog(@"原点%f原点%f",imgview.layer.position.x,imgview.layer.position.y);
	CGPathMoveToPoint(path, NULL,imgview.layer.position.x,imgview.layer.position.y);
	
	int p =  [self getblank:tag];
	NSLog(@"旋转%d",p);
	float f = 2.0*M_PI  - 2.0*M_PI *p/PHOTONUM;
	float h = f + 2.0*M_PI *num/PHOTONUM;
	float centery = self.view.center.y - 50;
	float centerx = self.view.center.x;
	float tmpy =  centery + RADIUS*cos(h);
	float tmpx =	centerx - RADIUS*sin(h);
	imgview.center = CGPointMake(tmpx,tmpy);
	
	CGPathAddArc(path,nil,self.view.center.x, self.view.center.y - 50,RADIUS,f+ M_PI/2,f+ M_PI/2 + 2.0*M_PI *num/PHOTONUM,0);	
	animation.path = path;
	CGPathRelease(path);
	animation.duration = TIME;
	animation.repeatCount = 1;
 	animation.calculationMode = @"paced"; 	
	return animation;
}
-(void)pageCurlAnmation:(UIView*)view
{
    CATransition *tran=[CATransition animation];
//    tran.type=@"pageCurl";//pageCurl／flip
//tran.type =@"suckEffect";
if (currenttag ==1002)
 {
  tran.type = @"suckEffect";//左上抽出
  
}
else

{
       tran.type = @"rippleEffect";

}
//    tran.subtype=@"fromleft";
    tran.duration=1.5;
    tran.delegate=self;
    [view.layer addAnimation:tran forKey:@"ads"];
}
//-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
//{
//                             BIDUserSaveToInternet *next=[[BIDUserSaveToInternet alloc] initWithNibName:@"BIDUserSave" bundle:nil];
//            UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:next];
//            [self presentViewController:nav animated:NO completion:nil];
//
//}

-(NSInteger)getblank:(NSInteger)tag
{
	if (currenttag>tag) 
	{
		return currenttag - tag;
	}
	else 
	{
		return PHOTONUM  - tag + currenttag;
	}

}

-(void)Scale
{
	[UIView beginAnimations:nil context:self];
	[UIView setAnimationRepeatCount:3];
    [UIView setAnimationDuration:1];	
	
	/*
	 + (void)setAnimationWillStartSelector:(SEL)selector;                // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
	 + (void)setAnimationDidStopSelector:(SEL)selector;                  // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
	 + (void)setAnimationDuration:(NSTimeInterval)duration;              // default = 0.2
	 + (void)setAnimationDelay:(NSTimeInterval)delay;                    // default = 0.0
	 + (void)setAnimationStartDate:(NSDate *)startDate;                  // default = now ([NSDate date])
	 + (void)setAnimationCurve:(UIViewAnimationCurve)curve;              // default = UIViewAnimationCurveEaseInOut
	 + (void)setAnimationRepeatCount:(float)repeatCount;                 // default = 0.0.  May be fractional
	 + (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;
	 */
	
	CATransform3D rotationTransform = CATransform3DIdentity;

    rotationTransform = CATransform3DRotate(rotationTransform,3.14, 1.0, 0.0, 0.0);	
	//rotationTransform = CATransform3DScale (rotationTransform, 0.1,0.1, 2);
    //self.view.transform=CGAffineTransformMakeScale(2,2);
	
	self.view.layer.transform=rotationTransform;
    [UIView setAnimationDelegate:self];	
    [UIView commitAnimations];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=YES;
        //[self.navigationController.viewControllers objectAtIndex:<#(NSUInteger)#>]
}
-(void)startAnimation
{
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    animation.duration=1;
    animation.repeatCount=0;
    animation.removedOnCompletion=NO;
    animation.autoreverses=NO;
    animation.fillMode=kCAFillModeForwards;
    animation.fromValue=[NSNumber numberWithInt:-480];
    animation.toValue=[NSNumber numberWithInt:0];
    [self.view.layer addAnimation:animation forKey:@"transfrom"];
}
- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
       [playmusic release];
    [super dealloc];
}

-(void)play
{
         NSError * error;
	   NSString * stringPath = [[NSBundle mainBundle] pathForResource:@"clock.mp3" ofType:nil];
	   playmusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:stringPath] error:&error];
       	   playmusic.delegate = self;
       NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(NSTimeInterval) userInfo:self repeats:NO];
       NSLog(@"%@",timer);

//[playmusic setCurrentTime:10.f];
       [playmusic play];

//       if (!playmusic.playing)
//       {
//              [playmusic prepareToPlay];
////                [playmusic setCurrentTime:2.0f];
////              double f=playmusic.duration;
//              playmusic.duration
//                [playmusic play];
//              
////              [self stop];
//
//
//       }

}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags
{

       [self Clickup:currenttag];
       if (flags == currenttag)
        {
              [self stop];
       }
       else
       {
              [self play];
       }
}
-(void)stop
{
       [playmusic pause];
}
@end
